package cn.tedu.controller;

import cn.tedu.dao.HttpDao;
import cn.tedu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HttpController {
    @Autowired
    private HttpDao dao;

    //value表示访问路径,method请求方式
    @RequestMapping(value = "getUser",method = RequestMethod.GET)
    public String getUserAll() {
        List<User> list = dao.getUserAll();
        return list.toString();
    }

    @RequestMapping(value = "getUserById",method = RequestMethod.GET)
    public User getUser(int id) {
        User user = dao.getUser(id);
        return user;
    }

    //@RequestBody post中从请求实体中取值
    @RequestMapping(value = "saveUser",method = RequestMethod.POST)
    public String posetUser(@RequestBody User user) {
        dao.postUser(user);
        return "添加成功!";
    }

    @RequestMapping(value = "/updateUser",method = RequestMethod.PUT)
    public String putUser(@RequestBody User user){
        dao.putUser(user);
        return "修改成功";
    }

    @RequestMapping(value = "/deleteUser",method = RequestMethod.DELETE)
    public String deleteUser(int id){
        dao.deleteUser(id);
        return "删除成功";
    }
}
